
//
//  OrganationBottomCell.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/8/14.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "OrganationBottomCell.h"
#import "InstituteModel.h"
#import "CatagoryModel.h"
#import <MapKit/MapKit.h>
#import <BaiduMapAPI_Map/BMKMapView.h>
@implementation OrganationBottomCell


-(void)setModel:(InstituteModel *)model{
    _model = model;
    UIImageView *iconImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:iconImageView];
    [iconImageView sd_setImageWithURL:[NSURL URLWithString:_model.imgPath] placeholderImage:[UIImage imageNamed:@""]];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).offset(scaleLeftAndRight(10));
        make.width.mas_equalTo(scaleLeftAndRight(100));
        make.top.equalTo(self.contentView.mas_top).offset(scaleTopAndBottom(10));
        make.bottom.equalTo(self.contentView.mas_bottom).offset(scaleTopAndBottom(-10));
    }];
    UILabel *titleLabel = [[UILabel alloc] init];
    [self.contentView addSubview:titleLabel];
    titleLabel.text = _model.name;
    titleLabel.font = [UIFont systemFontOfSize:15];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageView.mas_right).offset(scaleLeftAndRight(5));
            make.width.mas_equalTo(scaleLeftAndRight(200));
        make.top.equalTo(self.contentView.mas_top).offset(scaleTopAndBottom(10));
            make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    UILabel *tallyLabel1 = [[UILabel alloc] init];
    [self.contentView addSubview:tallyLabel1];
    CatagoryModel *modelss = _model.institutionCategorys[0];
    tallyLabel1.text = modelss.name;
    tallyLabel1.textAlignment = NSTextAlignmentCenter;
    tallyLabel1.textColor = COLOR_HEX(0xf9bc3d);
    tallyLabel1.backgroundColor = COLOR_HEX(0xfdf5e0);
    tallyLabel1.font = [UIFont systemFontOfSize:13];
    [tallyLabel1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(iconImageView.mas_right).offset(scaleLeftAndRight(5));
        make.width.mas_equalTo(scaleLeftAndRight(65));
        make.top.equalTo(titleLabel.mas_bottom).offset(scaleTopAndBottom(10));
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    tallyLabel1.layer.borderColor = COLOR_HEX(0xf9bc3d).CGColor;
    tallyLabel1.layer.borderWidth = 0.5f;
    tallyLabel1.layer.cornerRadius = 4;
    tallyLabel1.layer.masksToBounds = YES;
    if (_model.institutionCategorys.count > 1) {
        UILabel *tallyLabel2 = [[UILabel alloc] init];
        [self.contentView addSubview:tallyLabel2];
        CatagoryModel *modelsss = _model.institutionCategorys[1];
        tallyLabel2.text = modelsss.name;
        tallyLabel2.backgroundColor = COLOR_HEX(0xfdf5e0);
        tallyLabel2.textColor = COLOR_HEX(0xf9bc3d);
        tallyLabel2.textAlignment = NSTextAlignmentCenter;
        tallyLabel2.font = [UIFont systemFontOfSize:13];
        [tallyLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(tallyLabel1.mas_right).offset(scaleLeftAndRight(10));
            make.width.mas_equalTo(scaleLeftAndRight(65));
            make.top.equalTo(titleLabel.mas_bottom).offset(scaleTopAndBottom(10));
            make.height.mas_equalTo(scaleTopAndBottom(20));
        }];
        tallyLabel2.layer.borderColor = COLOR_HEX(0xf9bc3d).CGColor;
        tallyLabel2.layer.borderWidth = 0.5f;
        tallyLabel2.layer.cornerRadius = 4;
        tallyLabel2.layer.masksToBounds = YES;
        if (_model.institutionCategorys.count > 2) {
            UILabel *tallyLabel3 = [[UILabel alloc] init];
            [self.contentView addSubview:tallyLabel3];
            CatagoryModel *modelsss = _model.institutionCategorys[2];
            tallyLabel3.text = modelsss.name;
            tallyLabel3.textColor = COLOR_HEX(0xf9bc3d);
            tallyLabel3.backgroundColor = COLOR_HEX(0xfdf5e0);
            tallyLabel3.textAlignment = NSTextAlignmentCenter;
            tallyLabel3.font = [UIFont systemFontOfSize:13];
            [tallyLabel3 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(tallyLabel2.mas_right).offset(scaleLeftAndRight(10));
                make.width.mas_equalTo(scaleLeftAndRight(65));
                make.top.equalTo(titleLabel.mas_bottom).offset(scaleTopAndBottom(10));
                make.height.mas_equalTo(scaleTopAndBottom(20));
            }];
            tallyLabel3.layer.borderColor = COLOR_HEX(0xf9bc3d).CGColor;
            tallyLabel3.layer.borderWidth = 0.5f;
            tallyLabel3.layer.cornerRadius = 4;
            tallyLabel3.layer.masksToBounds = YES;
        }

    }
    UIView *starView = [[UIView alloc] init];
    [self.contentView addSubview:starView];
    [starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom).offset(scaleTopAndBottom(-10));
        make.left.equalTo(iconImageView.mas_right).offset(scaleLeftAndRight(5));
        make.width.mas_equalTo(120);
        make.height.mas_equalTo(scaleTopAndBottom(20));
    }];
    NSMutableArray *starArra = [[NSMutableArray alloc] init];
    for (NSInteger i = 0; i < 5; i++) {
        UIImageView *starImageView = [[UIImageView alloc] init];
        starImageView.image = [UIImage imageNamed:@"b27_icon_star_gray"];
        [starView addSubview:starImageView];
        [starArra addObject:starImageView];
    }
    [starArra mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedItemLength:20 leadSpacing:0 tailSpacing:0];
    [starArra mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(20);
        make.centerY.mas_equalTo(starView.mas_centerY);
    }];
    for (NSInteger i = 0; i < [model.star integerValue]; i++) {
        UIImageView *imageView = starArra[i];
        imageView.image = [UIImage imageNamed:@"b27_icon_star_yellow"];
    }
    
//    _detailbtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [self.contentView addSubview:_detailbtn];
//    [_detailbtn setTitle:@"详情>" forState:UIControlStateNormal];
//    [_detailbtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
//    _detailbtn.titleLabel.font = [UIFont systemFontOfSize:13];
//    [_detailbtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.contentView.mas_top).offset(scaleTopAndBottom(10)); make.left.equalTo(titleLabel.mas_right).offset(scaleLeftAndRight(5)); make.right.equalTo(self.contentView.mas_right).offset(scaleLeftAndRight(-10));
//        make.height.mas_equalTo(scaleTopAndBottom(20));
//    }];
    _collectbtn = [UIButton buttonWithType:UIButtonTypeCustom];
    if ([model.province isEqual:model.city]) {
            [self ForwardCode:[NSString stringWithFormat:@"%@%@%@",model.city,model.area,model.addresss]];
    }else{
            [self ForwardCode:[NSString stringWithFormat:@"%@%@%@%@",model.province,model.city,model.area,model.addresss]];
    }
    self.collectbtn.titleLabel.font = [UIFont systemFontOfSize:13];
    [_collectbtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [_collectbtn setImage:[UIImage imageNamed:@"instilocal"] forState:UIControlStateNormal];
    [self.contentView addSubview:_collectbtn];
    [_collectbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.contentView.mas_bottom).offset(scaleTopAndBottom(-10));
        make.right.equalTo(self.contentView.mas_right).offset(scaleLeftAndRight(-10));
        make.height.mas_equalTo(20);
        make.left.equalTo(starView.mas_right).offset(scaleLeftAndRight(15));
    }];

}
//正向地理编码
-(void)ForwardCode:(NSString *)str{
    //编码对象
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    //正向地理编码
    [geoCoder geocodeAddressString:str completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count == 0 || error) {
            NSLog(@"编码失败%@",error);
//            NSLog(@"%@",str);
            return ;
        }
        CLPlacemark *placeMark = [placemarks lastObject];
        NSLog(@"%lf--%lf",placeMark.location.coordinate.latitude,placeMark.location.coordinate.longitude);
        NSString *latitu = [NSString stringWithFormat:@"%f",placeMark.location.coordinate.latitude];
        NSString *longtitu = [NSString stringWithFormat:@"%f",placeMark.location.coordinate.longitude];
       NSString *tile = [self getDistanceWithLatitu:latitu AndLongtitu:longtitu];
        [_collectbtn setTitle:[NSString stringWithFormat:@"  距离%@",tile] forState:UIControlStateNormal];
        
}];
}
-(NSString *)getDistanceWithLatitu:(NSString *)latitu AndLongtitu:(NSString *) longtitu{
    CLLocationCoordinate2D coor[2] = {0};
    coor[0].latitude = [latitu doubleValue];
    coor[0].longitude = [longtitu doubleValue];
    NSString *cureentLatitu = [[NSUserDefaults standardUserDefaults] objectForKey:@"latitude"];
    NSString *cureentLongtitu = [[NSUserDefaults standardUserDefaults] objectForKey:@"longtitude"];
    coor[1].latitude = [cureentLatitu doubleValue];
    coor[1].longitude = [cureentLongtitu doubleValue];
    CLLocation *send = [[CLLocation alloc] initWithLatitude:coor[0].latitude longitude:coor[0].longitude];
    CLLocation *current = [[CLLocation alloc] initWithLatitude:coor[1].latitude longitude:coor[1].longitude];
    CLLocationDistance dis = [send distanceFromLocation:current];
     NSString *disSendStr = [NSString stringWithFormat:@"%.0lfkm",dis/1000.00];
    return disSendStr;
}
@end
